/**
 * Created by dongasai on 16-10-7.
 */
class LoadJS{
    constructor($config){
        this.queue=[];
        this.already={};//已经加载过的
        this.ready={};
        this.configing($config)
        this.head = document.getElementsByTagName('head');
    }
    configing($config){
        this.base=$config.base;
        if(typeof $config.module === 'undefined'){
            this.modules={};
        }else{
            this.modules=$config.module;
        }
    }
    module($name,$config){
        this.modules[$name]=this.module_config($config);
    }
    module_config($config){
        if(typeof $config.Require_base === 'undefined'){
            $config.Require_base=true;
        }
        return  $config;
    }

    all($list,$name='re'){

        for(let li in  $list) {
            this.one($list[li]);
        }
        this.queue.push($name);
    }
    /**
     * 加载一个扩展 并且只加载一次
     * @param $name
     */
    one($name){

        if(typeof $name === 'object'){
            if(typeof $name.src[0] === 'undefined'){
                console.log('资源配置错误!:',$name);
                return false;
            }
            this.module($name.src[0],$name)
            $name=$name.src[0];
        }

        if(this.already[$name]===true){
            //这个资源已经加载了一次了
            console.log('这个资源已经加载了一次了:',$name);
            return;
        }
        let scriptobj;

        if(typeof this.modules[$name] === 'undefined'){
            //没有定义该扩展
             scriptobj=this.analysis($name);
        }else{
            //扩展已经定义
             scriptobj=this.modules[$name];
        }
        let srclist=this.dompat(scriptobj);
        this.insert(srclist);
        this.already[$name]=true;
    }
    insert(srclist){
        for(let vla of srclist){

            if(vla[1]==='js'){
                this.insert_js(vla);
            }
            if(vla[1]==='css'){
                this.insert_css(vla);
            }

        }
    }
    insert_js(vla){
        let $this=this;
        //动态加载
        let scriptsq = document.createElement("SCRIPT");
        scriptsq.type =  'text/javascript';
        // scriptsq.defer = "defer";
        scriptsq.src = vla[0];//设置路径

        scriptsq.addEventListener('load',function(){$this.onload(this)});
        LoadJS.addClass(scriptsq,'RequireJS');//设置代表加载中的 class
        this.head[0].appendChild(scriptsq);
    }
    insert_css(vla){
        let $this=this;
        //动态加载
        let scriptsq = document.createElement("LINK");
        scriptsq.rel =  'stylesheet';
        scriptsq.href = vla[0];//设置路径
        scriptsq.addEventListener('load',function(){$this.onload(this)});
        LoadJS.addClass(scriptsq,'RequireJS');//设置代表加载中的 class

        this.head[0].appendChild(scriptsq);

    }

    dompat(scriptobj){
        let listarr=[],srcstr='',arr;
        if(typeof(scriptobj.src) === 'string'){
            //一条记录
            srcstr=this.Require_base_src(scriptobj.src,scriptobj.Require_base);
            arr=[
                srcstr,this.gettype()
            ];
            listarr.push(arr);
            return listarr;
        }else{
            //多条记录
            for(let key in (scriptobj.src)){
                srcstr=this.Require_base_src(scriptobj.src[key][0],scriptobj.Require_base);
                arr=[
                    srcstr,this.gettype(scriptobj.src[key][1])
                ];
                listarr.push(arr);
            }
            return listarr;
        }


        return ;
    }
    gettype(type='js'){
        if(typeof type ==='undefined'){
            return 'js';
        }
        return type;
    }
    Require_base_src(src,Require_base){
        if(Require_base){
            //base 前缀
            return this.base+src;
        }else{
            //没有base 前缀
            return  src;
        }
    }
    analysis($name){
        let obj={
            'Require_base':false,//从base路径加载 默认为 true
            'src':this.base+$name
        }

        return obj;
    }
    srcpat(obj){

    }

    onload(thisob){
        LoadJS.removeClass(thisob,'RequireJS');
        //减掉这个class
        let x =document.getElementsByClassName('RequireJS');
        if(x.length === 0){

            this.loadok();
        }
    }
    loadok(){

        for(let li in this.queue){
            if(typeof this.ready[this.queue[li]] === 'undefined'){
                //没有定义就继续走
                console.log(this.queue[li]+'没有后续方法!');
            }else{
                this.ready[this.queue[li]].call();
            }
        }
        this.queue=[];
    }
     static hasClass(obj, cls) {
        return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
    }
    static addClass(obj, cls) {
        if (!LoadJS.hasClass(obj, cls)) obj.className += " " + cls;
    }
    static removeClass(obj, cls) {
        if (LoadJS.hasClass(obj, cls)) {
            var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
            obj.className = obj.className.replace(reg, ' ');
        }
    }
}